package com.sgb.jicai.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.jicai.BR;
import com.sgb.jicai.R;
import com.sgb.link.entity.GetSkuEntity;
import com.sgb.link.entity.GoodsSkuChild;
import com.swgk.core.recyclerview.FlowLayoutManager;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/7/28
 * AUTHOR:F_Q
 * Description:规格型号的adapter
 */
public class ProductModelAdaptrer extends BaseRecycleAdapter<GetSkuEntity> {

    private ProductModelTwoAdaptrer adaptrer;

    private Map<Integer, HashSet<GoodsSkuChild>> mSetMap;

    public ProductModelAdaptrer(Context context, List<GetSkuEntity> datas) {
        super(context, R.layout.item_jicai_product_model, datas);
        this.context = context;
        mSetMap = new HashMap<>();
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        CheckBox checkBox = holder.itemView.findViewById(R.id.materials_select);
        checkBox.setOnCheckedChangeListener(null);
        HashSet<GoodsSkuChild> skuChildren = mSetMap.get(holder.getLayoutPosition());
        if (skuChildren != null && skuChildren.size() == datas.get(position).getList().size()) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                datas.get(position).setIsonclick(isChecked);
                for (GoodsSkuChild goodsSkuChild : datas.get(position).getList()) {
                    goodsSkuChild.setOnClick(isChecked);
                    saveSelected(goodsSkuChild, holder.getPosition());
                }
                notifyDataSetChanged();
                OperationData.OperationData(mSetMap);
            }
        });
        adapterViewData(holder, datas.get(position));
    }

    @Override
    public void convert(ItemViewHolder holder, GetSkuEntity entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    private void adapterViewData(ItemViewHolder holder, GetSkuEntity entity) {
        RecyclerView recyclerview = holder.itemView.findViewById(R.id.recyclerview);
        adaptrer = new ProductModelTwoAdaptrer(context, entity.getList());
        recyclerview.setLayoutManager(new FlowLayoutManager());
        recyclerview.setAdapter(adaptrer);
        adaptrer.setCheckListener(new ProductModelTwoAdaptrer.ProductModelChildCheckListener() {
            @Override
            public void onChildCheckListener(int childPos, GoodsSkuChild child) {
                saveSelected(child, holder.getPosition());
                OperationData.OperationData(mSetMap);
            }
        });
    }

    public void saveSelected(GoodsSkuChild child, int holder) {
        HashSet<GoodsSkuChild> children = mSetMap.get(holder);
        if (children == null) {
            children = new HashSet<>();
        }
        if (child.isOnClick()) {
            children.add(child);
        } else {
            children.remove(child);
        }
        mSetMap.put(holder, children);
        notifyItemChanged(holder);
    }

    private OperationData OperationData;

    public void setOperationData(OperationData operationData) {
        OperationData = operationData;
    }

    public interface OperationData {
        void OperationData(Map<Integer, HashSet<GoodsSkuChild>> setMap);
    }
}
